package com.klm.easymq.controller;

import com.klm.easymq.entity.Message;
import com.klm.easymq.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Easy-MQ 测试控制器
 * @author klm
 */
@RestController
@RequestMapping("/easymq/test")
public class EasyMqTestController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private MessageService messageService;

    @GetMapping("/health")
    public Map<String, Object> health() {
        Map<String, Object> result = new HashMap<>();
        result.put("status", "UP");
        result.put("timestamp", LocalDateTime.now());
        result.put("service", "easy-mq");
        result.put("type", "SDK Service");
        result.put("database", "SQLite (Single DataSource)");
        return result;
    }

    @GetMapping("/hello")
    public String hello() {
        return "Hello Easy MQ SDK with Single SQLite DataSource!";
    }

    @GetMapping("/info")
    public Map<String, Object> info() {
        Map<String, Object> info = new HashMap<>();
        info.put("serviceName", "easy-mq");
        info.put("version", "1.0.0");
        info.put("description", "Easy MQ SDK Service with Single SQLite DataSource");
        info.put("startTime", LocalDateTime.now());
        info.put("database", "SQLite");
        info.put("dataSourceType", "Single DataSource");
        info.put("features", new String[]{"SDK Service", "Single SQLite Database", "Redis Cache", "REST API"});
        return info;
    }

    @GetMapping("/db-test")
    public Map<String, Object> testDatabase() {
        Map<String, Object> result = new HashMap<>();
        try {
            // 测试数据库连接
            String sql = "SELECT COUNT(*) FROM messages";
            Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
            
            result.put("success", true);
            result.put("message", "数据库连接正常");
            result.put("messageCount", count);
            result.put("databaseFile", "data/easy-mq.db");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "数据库连接失败: " + e.getMessage());
            result.put("error", e.getClass().getSimpleName());
        }
        return result;
    }

    @GetMapping("/insert-test-data")
    public Map<String, Object> insertTestData() {
        Map<String, Object> result = new HashMap<>();
        try {
            // 创建测试消息
            Message testMessage = new Message();
            testMessage.setTraceId(UUID.randomUUID().toString());
            testMessage.setTopic("test-topic");
            testMessage.setStatus("SUCCESS");
            testMessage.setConsumeTime(100L);
            testMessage.setErrorMsg(null);
            testMessage.setCreateTime(LocalDateTime.now());

            // 保存测试消息
            boolean success = messageService.save(testMessage);
            
            if (success) {
                result.put("success", true);
                result.put("message", "测试数据插入成功");
                result.put("data", testMessage);
            } else {
                result.put("success", false);
                result.put("message", "测试数据插入失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "测试数据插入异常: " + e.getMessage());
            result.put("error", e.getClass().getSimpleName());
        }
        return result;
    }
} 